package com.zijiebutiao.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijiebutiao.pojos.Result;
import com.zijiebutiao.pojos.Setmeal;
import com.zijiebutiao.pojos.TCheckgroup;
import com.zijiebutiao.service.SetmealService;
import com.zijiebutiao.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @GetMapping("/{page}/{size}")
    public com.zijiebutiao.pojos.Result findByPage(@PathVariable int page, @PathVariable int size){
        IPage<Setmeal> ipage=new Page<>(page,size);
        setmealService.page(ipage);
       return Result.success(ipage);
    }
    @PostMapping("/add.do")
    public Result addSetmeal(String checkgroupIds,@RequestBody Setmeal setmeal){
        String[] split=null;
        if (checkgroupIds!=null&&checkgroupIds.length()!=0){
            split=checkgroupIds.split(",");

        }
        setmealService.saveSetmeal(setmeal,split);
       return Result.success(null);
    }
    @Transactional
    @PostMapping("/doedit")
    public ResultVo doedit(@RequestBody Setmeal id) {
        ResultVo resultVo = new ResultVo();
        Setmeal setmeal = setmealService.getById(id.getId());
        Wrapper<Setmeal> wq = Wrappers.lambdaQuery(Setmeal.class)
                .ne(Setmeal::getId, setmeal.getId());
        for (Setmeal checkitem : setmealService.list(wq)) {
            if (checkitem.getId().equals(id.getId())) {
                resultVo.setCode(403);
                resultVo.setErrorMsg("编码重复修改失败");
                return resultVo;
            }
        }
        Wrapper<Setmeal> qw=Wrappers.lambdaQuery(Setmeal.class)
                .eq(Setmeal::getId,id.getId());
        setmealService.update(id,qw);
        resultVo.setErrorMsg("修改成功");
        return resultVo;
    }
    @Transactional
    @GetMapping("/del")
    public ResultVo del(String id) {
        ResultVo resultVo = new ResultVo();
        Setmeal byId = setmealService.getById(id);
        if (byId != null) {
            setmealService.removeById(id);
            resultVo.setErrorMsg("删除成功");
        } else {
            resultVo.setErrorMsg("删除失败");
            resultVo.setCode(403);
        }
        return resultVo;
    }
//    @PostMapping("doedit")
//    public ResultVo dpoEdit(@RequestBody Setmeal setmeal){
//        ResultVo vo=new ResultVo();
//        int result= setmealService.editSetmeal(setmeal);
//        vo.setData(result);
//        if (result==0){
//            vo.setCode(500);
//            vo.setErrorMsg("修改失败");
//        }else {
//            vo.setCode(200);
//            vo.setErrorMsg("修改成功");
//        }
//        return vo;
//    }
//    @GetMapping("del")
//    public ResultVo del(@RequestBody Setmeal setmeal){
//        ResultVo vo=new ResultVo();
//        int result=setmealService.removeSetmeal(setmeal);
//        vo.setData(result);
//        if(result==0){
//            vo.setCode(500);
//            vo.setErrorMsg("删除失败");
//        }else {
//            vo.setCode(200);
//            vo.setErrorMsg("删除成功");
//        }
//        return vo;
//    }
}
